import { createRouter, createWebHashHistory, RouteRecordRaw, RouterOptions } from "vue-router"
import Layout from "@/components/layout/Index.vue"

export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: 'home',
    component: () => import('@/views/home/Index.vue'),
    name: 'Home',
    meta: {
      title: '首页',
      icon: 'hamburger'
    },
    children:[
      {
        path: 'a1',
        component: () => import('@/views/home/Index.vue'),
        name: 'A1',
        meta: {
          title: 'A1',
          icon: 'user'
        },
      },
      {
        path: 'a2',
        component: () => import('@/views/home/Index.vue'),
        name: 'A2',
        meta: {
          title: 'A2',
          icon: 'wechat'
        },
      },
      {
        path: 'a3',
        component: () => import('@/views/home/Index.vue'),
        name: 'A3',
        meta: {
          title: 'A3',
          icon: 'user'
        },
      }
    ]
  },
  {
    path: 'login',
    component: () => import('@/views/login/Index.vue'),
    name: 'Login',
    meta: {
      title: 'Login',
      icon: 'download'
    }
  },
  {
    path: 'two',
    component: () => import('@/views/home/Index.vue'),
    name: 'Two',
    meta: {
      title: 'Two',
      icon: 'user',
      noCache: true,
      affix: true
    }
  }
]



export const routes: RouteRecordRaw[] = [
  // {
  //   path: '/redirect',
  //   component: Layout,
  //   meta: {
  //     hidden: true
  //   },
  //   children: [{
  //     path: '/redirect/:path(.*)',
  //     component: () => import('@/views/base/redirect.vue')
  //   }]
  // },
  // {
  //   path: '/login',
  //   name: 'login',
  //   component: () => import('@/views/login/Index.vue'),
  //   meta: {
  //     hidden: true,
  //     noCache: true
  //   }
  // },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      hidden: true,
      noCache: true
    }
  },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/error/401.vue'),
    meta: {
      hidden: true,
      noCache: true
    }
  },
  {
    path: '/',
    component: Layout,
    name:"Layout",
    redirect: 'home',
    meta: {
      hidden: true
    },
    children: asyncRoutes
  }
]


const routerParams: RouterOptions = {
  // history: createWebHashHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    return { left: 0, top: 0 }
  }
}

const router = createRouter(routerParams)

// 去除重复路由报错的问题
const originalPush = router.push
router.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export function resetRouter() {
  const newRouter = createRouter(routerParams)
  router.resolve = newRouter.resolve
}

export default router
